package com.lian.community.service.like;

import com.lian.community.constant.RedisKeyType;
import com.lian.community.dao.redis.JedisDAO;
import com.lian.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 赞与踩
 * 用reids的set实现，因为只要实现赞，踩，已经统计赞踩的个数
 * set是一个无序集合，可以存储多个val
 */
@Service
public class LikeService {

    @Autowired
    JedisDAO jedisDAO;



    public Long like(int entityId, int entityType, int userId){
            String key = RedisKeyUtil.get(entityId, entityType, RedisKeyType.LIKE);
            jedisDAO.sadd(key, String.valueOf(userId));
            String disLikeKey = RedisKeyUtil.get(entityId, entityType, RedisKeyType.DISLIKE);
            jedisDAO.srem(disLikeKey, String.valueOf(userId));
        return jedisDAO.scard(key);
    }

    public Long disLike(int entityId, int entityType, int userId){
            String key = RedisKeyUtil.get(entityId, entityType, RedisKeyType.DISLIKE);
            jedisDAO.sadd(key, String.valueOf(userId));
            String likeKey = RedisKeyUtil.get(entityId, entityType, RedisKeyType.LIKE);
            jedisDAO.srem(likeKey, String.valueOf(userId));
        return jedisDAO.scard(key);
    }


}
